package com.caiya.service.impl;

import com.caiya.cart.CartRequest;
import com.caiya.cart.entity.CartServiceType;
import com.caiya.cart.vo.servicetype.ServiceTypeAddVo;
import com.caiya.cart.vo.servicetype.ServiceTypeListVo;
import com.caiya.cart.vo.servicetype.ServiceTypeUpdateVo;
import com.caiya.mapper.CartServiceTypeMapper;
import com.caiya.service.CartServiceTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by Administrator on 2018/12/3 0003.
 */
@Service
public class CartServiceTypeServiceImpl implements CartServiceTypeService{

    @Autowired
    private CartServiceTypeMapper cartServiceTypeMapper;

    @Override
    public PageInfo<CartServiceType> list(ServiceTypeListVo serviceTypeListVo) {
        PageHelper.startPage(serviceTypeListVo.getPageNum(),serviceTypeListVo.getPageSize());
        Example example = new Example(CartServiceType.class);
        example.orderBy("sort").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",serviceTypeListVo.getParentId());
        List<CartServiceType> list = cartServiceTypeMapper.selectByExample(example);
        PageInfo<CartServiceType> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public int add(ServiceTypeAddVo serviceTypeAddVo) throws Exception {
        if (serviceTypeAddVo.getTypeName()==null || serviceTypeAddVo.getTypeName().trim().isEmpty()){
            throw new Exception("请填写类型名");
        }

        CartServiceType type = new CartServiceType();
        type.setTypeName(serviceTypeAddVo.getTypeName());
        type.setSort(Integer.valueOf(serviceTypeAddVo.getSort()));
        type.setParentId(Long.valueOf(serviceTypeAddVo.getParentId()));
        return cartServiceTypeMapper.insert(type);
    }

    @Override
    public int update(ServiceTypeUpdateVo serviceTypeUpdateVo) throws Exception {
        if (serviceTypeUpdateVo.getTypeName()==null || serviceTypeUpdateVo.getTypeName().trim().isEmpty()){
            throw new Exception("请填写类型名");
        }
        return cartServiceTypeMapper.update(serviceTypeUpdateVo);
    }
}
